/*
  operators.h

   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.
 
   Created on: Jul 17, 2009
 
 */

#ifndef OPERATORS_H_
#define OPERATORS_H_

#include <stdlib.h>

#include <core/queues.h>

namespace crossEyed
{
namespace operators
{

using namespace core ;

template<class REP>
class TOperator
{
public:
   size_t N;

   //
   // Queue for frames
   //
   TDataQueue<REP> frameQueue ;

public:

   TOperator( size_t N ) : N( N ), frameQueue( N ) { };
   virtual ~TOperator() { };

   TDataQueue<REP> * getDataQueue( ) { return &frameQueue ; } ;

   //
   // Implement callable concept
   //
   inline void operator()()
   {
      eventLoop() ;
   }

   virtual void eventLoop() = 0 ;
};

template<class REP>
class TManagedFrameOperator : public TOperator<REP>
{
public:
   TManagedFrameOperator( size_t N, unsigned int width, unsigned int height );
   virtual ~TManagedFrameOperator();

   virtual void eventLoop() = 0 ;
};

template<class REP>
TManagedFrameOperator<REP>::TManagedFrameOperator( size_t N, unsigned int width, unsigned int height )
   : TOperator<REP>( N )
{
   //
   // Allocate output frames. Attach them to the output queue defined in TOperator<>.
   // We have to do this in the derived class as an operator does not necessarily
   // model ownership of its frames.
   //
   for( unsigned int i = 0; i < N; i ++ )
   {
      REP *pRep ;
      try
      {
         pRep = new REP( width, height ); ;
      }
      catch(std::bad_alloc& badAlloc)
      {
         XI_OOM_HARD;
      }

      this->frameQueue.swizzleWriterData( pRep ) ;

      //
      // The following method is somewhat overkill since it also deals with all kinds of
      // serialization issues. This is not necessary as this queue is written and read only from
      // within the operator thread.
      //
      this->frameQueue.advanceWriterDataSlot() ;
   }
}

template<class REP>
TManagedFrameOperator<REP>::~TManagedFrameOperator()
{
   //
   // Allocate output frames. Attach them to the output queue defined in TOperator<>.
   // We have to do this in the derived class as an operator does not necessarily
   // model ownership of its frames.
   //
   for( unsigned int i = 0; i < this->N; i ++ )
   {
      delete this->frameQueue.getWriterData() ;

      //
      // The following method is somewhat overkill since it also deals with all kinds of
      // serialization issues. This is not necessary as this queue is written and read only from
      // within the operator thread.
      //
      this->frameQueue.advanceWriterDataSlot() ;
   }
}

} }

#endif /* OPERATORS_H_ */
